import {defaultStyle} from "@/svgutil/default-style";
import * as Dt from '@/svgutil/draw-type';
//import {findObjectValue, findText} from "@/svgutil/comm-method";
const trackColor = {
    c1: '#808080',
    c2: '#f0f',
    c3: '#f00',
    c4: '#0f0',
    c5: '#00f',
}
export const defineData = function (values) {
    //左边偏移10，上偏移20
    // 方块 16 * 12 ， 左右间隔 4，上下间隔 2
    return {
        translate: [10.5, 20.5],
        style: {...defaultStyle},
        children: [
            border(),
            title(values),
            ...trackList(values),
            ...tips(),
        ]
    }
}
function tips(){
    let rowGap = 8;
    let h = 16;
    let w = 40;
    let offsetX = 380;
    let padding = 2;
    let tipList = [];
    for(let i=0;i< 5; i++){
        let tipIn = Dt.rect(offsetX +padding,padding + i * (h +rowGap),w-padding,h-padding);
        tipIn.setStyle({stroke: '',fill: trackColor['c'+(i+1)]});
        let tipOut = Dt.rect(offsetX,i * (h +rowGap),w,h, {fill: ''});
        tipList.push(tipIn,tipOut);
    }
    return tipList;
}
function trackList(){
    let offsetY = 40;
    //let offsetX = 20;
    let rowGap = 3;
    let colGap = 4;
    let w = 20;
    let h = 16;
    let trackArray = [];
    for(let i= 1; i< 140; i++){
        let rowIndex = (i-1) % 30;
        let colIndex = (i-1) / 30 | 0;
        let startX = (colIndex * 2 +1) * (colGap + w) + colIndex * w;
        let startY = offsetY + rowIndex * (rowGap + h) - h;
        let serialRect = Dt.rect(startX,startY,w,h,getSerialFillStyle(i));
        serialRect.setText(i,getTextStyle(i));
        serialRect.onClick = (param) => {
            window.alert('点击了' + param.drawParam.text);
        }

        let trackRect = Dt.rect(startX + w + colGap,startY,w,h,{fill: '#808080'});
        trackRect.tag = i;
        trackRect.checkStyle = checkStyle;
        trackArray.push(serialRect,trackRect);
    }
    return trackArray;
}
function checkStyle({drawParam, values}){
    if(drawParam.tag ===1){
        let fill;
        if(values.P001 &&values.P001.v < 10){
            fill = trackColor.c1;
        }else {
            fill = trackColor.c4;
        }
        if(values.P001 && values.P001.w === 1){
            if(values.P001.v > 20){
                fill = trackColor.c5;
            }else {
                fill = trackColor.c3;
            }
        }
        return {fill: fill};
    }
    return {};
}
function getSerialFillStyle(index){
    let serialStyle = {};
    if(index< 41){
        serialStyle.fill = 'rgb(145,145,145)';
        serialStyle.stroke = trackColor.c2;
    }else if(index < 74){
        serialStyle.fill = 'rgb(157,0,0)';
        serialStyle.stroke = trackColor.c2;
    }else if(index < 107){
        serialStyle.fill = trackColor.c2;
    }else {
        serialStyle.fill = 'rgb(236,64,127)';
    }

    return serialStyle;
}
function getTextStyle(index){
    let serialTextStyle = {fontSize: 10};
    if(index < 107){
        serialTextStyle.fill = '#fff';
    }else {
        serialTextStyle.fill = '#000';
    }
    if(index > 99){
        serialTextStyle.fontSize = 9;
    }
    return serialTextStyle;
}

function border(){
    return Dt.rect(0,0,370,620,{fill: '#bbb',strokeWidth: 3,stroke:'#f00'}).ignoreMouseover();
}
function title() {
    let title = Dt.rect(30,4, 300, 16, {fill: 'rgb(0,132,0)',stroke: 'none'});
    title.setText('炉 内 坯 料 信 息 跟 踪',{fill: '#fff',fontWeight: 'bold'});

    return title;
}